package com.sun;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppendReplacementTest0 {
    public static void main(String[] args){
        Pattern p = Pattern.compile("\\$\\{\\w+\\}");
        Matcher m = p.matcher("The ${java} book is ${java} program book ${java}");
        StringBuffer sb = new StringBuffer("开始匹配");

        if(m.find()){
            m.appendReplacement(sb,"python");
        }
        System.out.println(sb);

        /**
         * 错误
        if(m.find()){
            m.appendReplacement(sb,"${java}");
        }
         $、{ 、}都是特殊字符，需要用反义字符\,appendReplacement方法却将传入的字符串中的$当做特殊字符来处理了。结果产生异常。
         */
        if(m.find()){
            m.appendReplacement(sb,"\\$\\{java\\}");
        }
        System.out.println(sb);

        if(m.find()){
            m.appendReplacement(sb,"python");
        }
        System.out.println(sb);

        System.out.println(m.replaceAll("c++"));
    }
}
